package com.example.himalaya.presenters;

import com.example.himalaya.model.api.SearchApi;
import com.example.himalaya.view.callback.ISearchViewCallBack;
import com.example.himalaya.constants.ApiConstants;
import com.example.himalaya.constants.Constants;
import com.example.himalaya.presenters.interfaces.ISearchPresenter;
import com.example.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.Announcer;
import com.ximalaya.ting.android.opensdk.model.album.SearchAlbumList;
import com.ximalaya.ting.android.opensdk.model.announcer.AnnouncerList;
import com.ximalaya.ting.android.opensdk.model.track.SearchTrackList;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.model.word.HotWord;
import com.ximalaya.ting.android.opensdk.model.word.HotWordList;
import com.ximalaya.ting.android.opensdk.model.word.QueryResult;
import com.ximalaya.ting.android.opensdk.model.word.SuggestWords;

import java.util.ArrayList;
import java.util.List;

public class SearchPresenter implements ISearchPresenter {

    // Constant
    private String TAG = "SearchPresenter";


    // List
    // Callback
    private List<ISearchViewCallBack> mISearchViewCallBacks = new ArrayList<>();
    private List<Album> mAlbumListResult = new ArrayList<>();
    private List<Track> mTrackListResult = new ArrayList<>();
    private List<Announcer> mAnnouncerListResult = new ArrayList<>();

    // Tool
    private SearchApi mSearchApi;
    private String mCurrentKeyWord = null;
    private int mCurrentAlbumPage = 1;
    private int mCurrentTrackPage = 1;
    private int mCurrentAnnouncerPage = 1;

    public void setCurrentKeyword(String currentKeyword) {

        mCurrentKeyWord = currentKeyword;

        doSearchAlbum();
        doSearchTrack();
        doSearchAnnouncer();
    }


    // Flag
    private boolean mIsLoadMoreAlbum = false;
    private boolean mIsLoadMoreTrack = false;
    private boolean mIsLoadMoreAnnouncer = false;


    private SearchPresenter() {
        mSearchApi = SearchApi.getSearchApi();
    }

    private static SearchPresenter sSearchPresenter = null;


    public static SearchPresenter getSearchPresenter() {
        if (sSearchPresenter == null) {
            synchronized (SearchPresenter.class) {
                if (sSearchPresenter == null) {
                    sSearchPresenter = new SearchPresenter();
                }
            }
        }

        return sSearchPresenter;
    }

    @Override
    public void doSearchAlbum() {

        mCurrentAlbumPage = Constants.DEFAULT_PAGE;

        mAlbumListResult.clear();
        // 当网络不好的时候 用户会点击从新搜索

        searchAlbum(mCurrentKeyWord);
    }

    @Override
    public void doSearchTrack() {
        LogUtil.d(TAG, "mIsLoadMoreTrack -->" + mIsLoadMoreTrack);


        mCurrentTrackPage = Constants.DEFAULT_PAGE;

        mTrackListResult.clear();
        // 当网络不好的时候 用户会点击从新搜索

        searchTrack(mCurrentKeyWord);
    }

    @Override
    public void doSearchAnnouncer() {
        LogUtil.d(TAG, "doSearchAnnouncer -->" + mIsLoadMoreTrack);


        mCurrentAnnouncerPage = Constants.DEFAULT_PAGE;

        mAnnouncerListResult.clear();
        // 当网络不好的时候 用户会点击从新搜索

        searchAnnouncer(mCurrentKeyWord);
    }

    @Override
    public void loadMoreAlbum() {

        // 判断有没有必要加载更多
        if (mAlbumListResult.size() < Constants.SEARCH_NUMBER) {
            for (ISearchViewCallBack iSearchViewCallBack : mISearchViewCallBacks) {
                iSearchViewCallBack.onLoadMoreAlbumResult(mAlbumListResult, false);
            }

            return;
        }

        this.mCurrentAlbumPage++;
        mIsLoadMoreAlbum = true;

        if (mCurrentKeyWord != null) {
            searchAlbum(mCurrentKeyWord);
        }
    }

    @Override
    public void loadMoreTrack() {
        // 判断有没有必要加载更多
        if (mTrackListResult.size() < Constants.SEARCH_NUMBER) {
            for (ISearchViewCallBack iSearchViewCallBack : mISearchViewCallBacks) {
                iSearchViewCallBack.onLoadMoreTrackResult(mTrackListResult, false);
            }

            return;
        }

        this.mCurrentTrackPage++;
        mIsLoadMoreTrack = true;

        if (mCurrentKeyWord != null) {
            searchTrack(mCurrentKeyWord);
        }
    }

    @Override
    public void loadMoreAnnouncer() {
        // 判断有没有必要加载更多
        if (mAnnouncerListResult.size() < Constants.SEARCH_NUMBER) {
            for (ISearchViewCallBack iSearchViewCallBack : mISearchViewCallBacks) {
                iSearchViewCallBack.onLoadMoreAnnouncerResult(mAnnouncerListResult, false);
            }

            return;
        }

        this.mCurrentAnnouncerPage++;
        mIsLoadMoreAnnouncer = true;

        if (mCurrentKeyWord != null) {
            searchAnnouncer(mCurrentKeyWord);
        }
    }

    private void searchAnnouncer(String keyword) {
        mSearchApi.getSearchAnnouncers(keyword, mCurrentAnnouncerPage, Constants.SEARCH_NUMBER, ApiConstants.RELATED_ANNOUNCER, new IDataCallBack<AnnouncerList>() {

            @Override
            public void onSuccess(AnnouncerList announcerList) {
                List<Announcer> announcers = announcerList.getAnnouncerList();

                if (announcers != null) {
                    // 放到总的队列
                    mAnnouncerListResult.addAll(announcers);
                    LogUtil.d(TAG, "mIsLoadMoreTrack -->" + mIsLoadMoreAnnouncer);

                    if (mIsLoadMoreAnnouncer) {

                        for (ISearchViewCallBack iSearchViewCallBack : mISearchViewCallBacks) {
                            iSearchViewCallBack.onLoadMoreAnnouncerResult(mAnnouncerListResult, announcers.size() != 0);
                        }

                        mIsLoadMoreAnnouncer = false;
                    } else {
                        LogUtil.d(TAG, "searchTrack callback size-->" + mISearchViewCallBacks.size());

                        for (ISearchViewCallBack iSearchViewCallBack : mISearchViewCallBacks) {
                            iSearchViewCallBack.onSearchAnnouncerLoaded(announcers);
                        }
                    }

                } else {
//                    LogUtil.d(TAG, "albums is null");
                }
            }

            @Override
            public void onError(int errorCode, String errorMsg) {
                if (mIsLoadMoreAnnouncer) {
                    for (ISearchViewCallBack iSearchViewCallBack : mISearchViewCallBacks) {
                        iSearchViewCallBack.onLoadMoreAnnouncerResult(mAnnouncerListResult, false);
                    }

                    // 失败了不能增加页数
                    mCurrentAnnouncerPage--;

                    mIsLoadMoreAnnouncer = false;
                } else {
                    for (ISearchViewCallBack iSearchViewCallBack : mISearchViewCallBacks) {
                        iSearchViewCallBack.onError(errorCode, errorMsg);
                    }
                }
            }
        });
    }

    private void searchTrack(String keyword) {
        mSearchApi.getSearchedTracks(keyword, mCurrentTrackPage, Constants.SEARCH_NUMBER, new IDataCallBack<SearchTrackList>() {
            @Override
            public void onSuccess(SearchTrackList searchTrackList) {
                List<Track> tracks = searchTrackList.getTracks();

                if (tracks != null) {
                    // 放到总的队列
                    mTrackListResult.addAll(tracks);
                    LogUtil.d(TAG, "mIsLoadMoreTrack -->" + mIsLoadMoreTrack);

                    if (mIsLoadMoreTrack) {

                        for (ISearchViewCallBack iSearchViewCallBack : mISearchViewCallBacks) {
                            iSearchViewCallBack.onLoadMoreTrackResult(mTrackListResult, tracks.size() != 0);
                        }

                        mIsLoadMoreTrack = false;
                    } else {
                        LogUtil.d(TAG, "searchTrack callback size-->" + mISearchViewCallBacks.size());

                        for (ISearchViewCallBack iSearchViewCallBack : mISearchViewCallBacks) {
                            iSearchViewCallBack.onSearchTrackLoaded(tracks);
                        }
                    }

                } else {
//                    LogUtil.d(TAG, "albums is null");
                }
            }

            @Override
            public void onError(int errorCode, String errorMsg) {
                if (mIsLoadMoreTrack) {
                    for (ISearchViewCallBack iSearchViewCallBack : mISearchViewCallBacks) {
                        iSearchViewCallBack.onLoadMoreTrackResult(mTrackListResult, false);
                    }

                    // 失败了不能增加页数
                    mCurrentTrackPage--;

                    mIsLoadMoreTrack = false;
                } else {
                    for (ISearchViewCallBack iSearchViewCallBack : mISearchViewCallBacks) {
                        iSearchViewCallBack.onError(errorCode, errorMsg);
                    }
                }
            }
        });
    }

    private void searchAlbum(String keyword) {

        mSearchApi.getSearchedAlbums(keyword, mCurrentAlbumPage, Constants.SEARCH_NUMBER, new IDataCallBack<SearchAlbumList>() {
            @Override
            public void onSuccess(SearchAlbumList searchAlbumList) {

                List<Album> albums = searchAlbumList.getAlbums();

                if (albums != null) {
                    // 放到总的队列
                    mAlbumListResult.addAll(albums);

//                    LogUtil.d(TAG, "album size -->" + albums.size());

                    if (mIsLoadMoreAlbum) {

                        for (ISearchViewCallBack iSearchViewCallBack : mISearchViewCallBacks) {
                            iSearchViewCallBack.onLoadMoreAlbumResult(mAlbumListResult, albums.size() != 0);
                        }

                        mIsLoadMoreAlbum = false;
                    } else {
                        LogUtil.d(TAG, "searchAlbum   callback size-->" + mISearchViewCallBacks.size());
                        for (ISearchViewCallBack iSearchViewCallBack : mISearchViewCallBacks) {
                            iSearchViewCallBack.onSearchAlbumLoaded(albums);
                        }
                    }

                } else {
                    LogUtil.d(TAG, "albums is null");
                }


            }

            @Override
            public void onError(int errorCode, String errorMsg) {
//                LogUtil.d(TAG, "getSearchedAlbums errorCode -->" + errorCode);
//                LogUtil.d(TAG, "getSearchedAlbums errorMsg  -->" + errorMsg);

                if (mIsLoadMoreAlbum) {
                    for (ISearchViewCallBack iSearchViewCallBack : mISearchViewCallBacks) {
                        iSearchViewCallBack.onLoadMoreAlbumResult(mAlbumListResult, false);
                    }

                    // 失败了不能增加页数
                    mCurrentAlbumPage--;

                    mIsLoadMoreAlbum = false;
                } else {
                    for (ISearchViewCallBack iSearchViewCallBack : mISearchViewCallBacks) {
                        iSearchViewCallBack.onError(errorCode, errorMsg);
                    }
                }

            }
        });
    }

    @Override
    public void reSearch() {

    }


    @Override
    public void getHotWord() {
        // TOOD: 做一个热词缓存

        mSearchApi.getHotWords(new IDataCallBack<HotWordList>() {
            @Override
            public void onSuccess(HotWordList hotWordList) {
                if (hotWordList != null) {
                    List<HotWord> hotWords = hotWordList.getHotWordList();
//                    LogUtil.d(TAG, "hotWords size -->" + hotWords.size());

                    for (ISearchViewCallBack iSearchViewCallBack : mISearchViewCallBacks) {
                        iSearchViewCallBack.onHotWorldLoaded(hotWords);
                    }
                }
            }

            @Override
            public void onError(int errorCode, String errorMsg) {
                LogUtil.d(TAG, "getHotWords() errorCode -->" + errorCode);
                LogUtil.d(TAG, "getHotWords() errorMsg  -->" + errorMsg);

                for (ISearchViewCallBack iSearchViewCallBack : mISearchViewCallBacks) {
                    iSearchViewCallBack.onError(errorCode, errorMsg);
                }
            }
        });
    }

    @Override
    public void getRecommendWord(String keyword) {

        mSearchApi.getSuggestWord(keyword, new IDataCallBack<SuggestWords>() {
            @Override
            public void onSuccess(SuggestWords suggestWords) {
                if (suggestWords != null) {
                    List<QueryResult> keyWordList = suggestWords.getKeyWordList();

                    LogUtil.d(TAG, "keyWordList size -->" + keyWordList.size());

                    for (ISearchViewCallBack iSearchViewCallBack : mISearchViewCallBacks) {
                        iSearchViewCallBack.onRecommendWorldLoaded(keyWordList);
                    }
                }
            }

            @Override
            public void onError(int errorCode, String errorMsg) {
                LogUtil.d(TAG, "getSuggestWord() errorCode -->" + errorCode);
                LogUtil.d(TAG, "getSuggestWord() errorMsg  -->" + errorMsg);
            }
        });
    }

    @Override
    public void registerViewCallback(ISearchViewCallBack iSearchViewCallBack) {
        if (mISearchViewCallBacks != null) {
            mISearchViewCallBacks.add(iSearchViewCallBack);
        }
    }

    @Override
    public void unRegisterViewCallback(ISearchViewCallBack iSearchViewCallBack) {
        mISearchViewCallBacks.remove(iSearchViewCallBack);
    }
}
